﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using Seagull.BarTender.Print;
using System.Drawing.Printing;

namespace ConsoleApplication1
{
    public class printServiceImpl : PrintService
    {
        private static PrintDocument fPrintDocument = new PrintDocument();
        LabelFormatDocument btFormat;
  
        string aa = "";
        string bb = "";
        string cc = "";
        string dd = "";
        string ff = "";
        string ee = "";
        string gg = "";
        string hh = "";
        string ii = "";
        string spec = "";
        string time = "";
        string wattage = "";
        string quantity = "";
        private string item_name;
        private string lot_no;
        private string type;
        public string printUpLabel(string doc_no)
        {
       
            Printers printers = new Printers();


            // cboPrinters.SelectedItem = btFormat.PrintSetup.PrinterName;


            SQLServerDataBase sql = new SQLServerDataBase();
            DataSet ds = new DataSet();
            string sqlString = "SELECT A.UDF021, B.ITEM_SPECIFICATION,PLAN_QTY,B.ITEM_CODE,C.LOT_CODE FROM MO A LEFT JOIN ITEM B ON A.ITEM_ID = B.ITEM_BUSINESS_ID LEFT JOIN ITEM_LOT C ON A.ITEM_LOT_ID = C.ITEM_LOT_ID WHERE DOC_NO = '" + doc_no + "' ";
            sql.ExecuteNonDataSet(sqlString, out ds);

            DataTable table = ds.Tables[0];
            time = table.Rows[0][0].ToString();//time250304
            aa = changeString(time);
            aa = aa.Substring(2);
            bb = table.Rows[0][1].ToString();//功率

            string[] bbarr = bb.Split(' ');
            bb = bbarr[2];
            spec = bbarr[0];

            cc = getQty(spec) + "PCS";
            dd = table.Rows[0][3].ToString();//品名
            ee = table.Rows[0][4].ToString();//批次号
            printBtw("newSbq.txt", "newSbq");
            return "功率为:"+bb+ "时间为:" + aa + "数量:" + cc + "品名:" + bb + "批次号:" + ee;


        }
        public string printUpLabel(string doc_no,int copies)
        {
          
            Printers printers = new Printers();


            // cboPrinters.SelectedItem = btFormat.PrintSetup.PrinterName;


            SQLServerDataBase sql = new SQLServerDataBase();
            DataSet ds = new DataSet();
            string sqlString = "SELECT A.UDF021, B.ITEM_SPECIFICATION,PLAN_QTY,B.ITEM_CODE,C.LOT_CODE FROM MO A LEFT JOIN ITEM B ON A.ITEM_ID = B.ITEM_BUSINESS_ID LEFT JOIN ITEM_LOT C ON A.ITEM_LOT_ID = C.ITEM_LOT_ID WHERE DOC_NO = '" + doc_no + "' ";
            sql.ExecuteNonDataSet(sqlString, out ds);

            DataTable table = ds.Tables[0];
            time = table.Rows[0][0].ToString();//time250304
            aa = changeString(time);
            aa = aa.Substring(2);
            bb = table.Rows[0][1].ToString();//功率

            string[] bbarr = bb.Split(' ');
            bb = bbarr[2];
            spec = bbarr[0];

            cc = getQty(spec) + "PCS";
            dd = table.Rows[0][3].ToString();//品名
            ee = table.Rows[0][4].ToString();//批次号
            printBtw("newSbq.txt", "newSbq1", copies);
            return "功率为:" + bb + "时间为:" + aa + "数量:" + cc + "品名:" + bb + "批次号:" + ee;


        }
        SQLServerDataBase sql = new SQLServerDataBase();
        private string sbTime;

        public string oldtoNew(string codeStr, string printName)
        {
         
            Printers printers = new Printers();

            string[] strArr = codeStr.Split(' ');


            if (strArr[4].IndexOf("21") >= 0 || strArr[4].IndexOf("22") >= 0)
            {
                strArr[4] = strArr[4].Replace("21", "24");
                strArr[4] = strArr[4].Replace("22", "24");


            }

            if (strArr[4].IndexOf("A") >= 0)
            {
                StringBuilder sb = new StringBuilder(strArr[4]);
                sb[5] = 'L';
                lot_no = sb.ToString();
            }
            else
            {
                lot_no = strArr[4];//批次号
            }
            //  strArr[4] = strArr[4].Replace("A", "L");


            type = strArr[0].Substring(0, 2);
            string spec = strArr[0].Substring(2, 4);
            wattage = getPower(type, spec);
            quantity = getQty(spec);
            item_name = "P/N:" + strArr[0] + " " + strArr[1] + " " + strArr[2] + " " + strArr[3];
            //   lot_no = strArr[4];//批次号
            int year = Convert.ToInt32("20" + lot_no.Substring(0, 2));


            int week = Convert.ToInt32(lot_no.Substring(2, 2));
            var t = new DateTime(year, 1, 1).AddDays(week * 7);

            time = t.ToString("yyMMdd");
            sbTime = changeString(time,1);
            string result = "功率为:" + wattage + "时间为:" + time + "数量:" + quantity + "品名:" + item_name + "批次号:" + lot_no;

            printBtw_oldtonew("newSbq.txt", "newSbq", printName);
            sql.ExecuteNonQuery_ljh("insert into replace_recode (oldLabel,newLabel,time)values('" + codeStr + "','" + result + "','" + DateTime.Now + "')");
            Console.WriteLine("time"+DateTime.Now);
            return result;
        }

        //获取本机默认打印机名称
        public static String DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }
        public void printBtw_oldtonew(string FieldPath, string btw_path,string printName)
        {
            //lock (Program.engine)
            //{
                string path = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine(path);
                string serialNumber = "time|wattage|quantity|item_name|lot_no|u_int|addr|customer_name\r\n" + time + "|" + wattage + "|" + quantity + "|" + item_name + "|" + lot_no + "|" + time + "|" + sbTime + "|" + hh + "|" + ii;
                using (StreamWriter stream = new StreamWriter(path + "\\" + FieldPath))
                {
                    stream.Write(serialNumber);
                }
                //Lable 路径 

                string filePath = path + btw_path + ".btw";
         
                btFormat = printController.engine.Documents.Open(filePath);
                //如何传参 命名具名数据源 赋予对应的 key<>value
                //          btFormat.SubStrings["ProductName"].Value = “test1”;
                //传入打印机名称
                btFormat.PrintSetup.PrinterName = printName;
                //打印张数
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//Convert.ToInt32(print_sl_txt.Text);
                //     MessageBox.Show("开始打印");

                Messages messages;
                string appName = "下标打印任务";
                Result result = btFormat.Print(appName, 10000, out messages);
                string messageString = "\n\nMessages:";

                foreach (Message message in messages)
                {
                    messageString += "\n\n" + message.Text;
                }

                if (result == Result.Failure)
                    Console.Write("Print Failed" + messageString, appName);
                else
                    Console.Write("文本已成功发送到打印机" + messageString, appName);
          //  }

            //btFormat.Close(SaveOptions.DoNotSaveChanges);
            //Program.engine.Stop();




        }
        public void printBtw(string FieldPath, string btw_path,int copies)
        {
            lock (printController.engine)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine(path);
                string serialNumber = "time|wattage|quantity|item_name|lot_no|u_int|addr|customer_name\r\n" + aa + "|" + bb + "|" + cc.ToString() + "|" + dd + "|" + ee + "|" + time + "|" + gg + "|" + hh + "|" + ii;
                using (StreamWriter stream = new StreamWriter(path + "\\" + FieldPath))
                {
                    stream.Write(serialNumber);
                }
                //Lable 路径 

                string filePath = path + btw_path + ".btw";
                btFormat = printController.engine.Documents.Open(filePath);
                //如何传参 命名具名数据源 赋予对应的 key<>value
                //          btFormat.SubStrings["ProductName"].Value = “test1”;
                //传入打印机名称
                btFormat.PrintSetup.PrinterName = DefaultPrinter();
                //打印张数
                btFormat.PrintSetup.IdenticalCopiesOfLabel = copies;//Convert.ToInt32(print_sl_txt.Text);
                //     MessageBox.Show("开始打印");

                Messages messages;
                string appName = "下标打印任务";
                Result result = btFormat.Print(appName, 10000, out messages);
                string messageString = "\n\nMessages:";

                foreach (Message message in messages)
                {
                    messageString += "\n\n" + message.Text;
                }

                if (result == Result.Failure)
                    Console.Write("Print Failed" + messageString, appName);
                else
                    Console.Write("文本已成功发送到打印机" + messageString, appName);
            }

          



        }
        public void printBtw(string FieldPath, string btw_path)
        {
            lock (printController.engine)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine(path);
                string serialNumber = "time|wattage|quantity|item_name|lot_no|u_int|addr|customer_name\r\n" + aa + "|" + bb + "|" + cc.ToString() + "|" + dd + "|" + ee + "|" + time + "|" + gg + "|" + hh + "|" + ii;
                using (StreamWriter stream = new StreamWriter(path + "\\" + FieldPath))
                {
                    stream.Write(serialNumber);
                }
                //Lable 路径 

                string filePath = path + btw_path + ".btw";
                btFormat = printController.engine.Documents.Open(filePath);
                //如何传参 命名具名数据源 赋予对应的 key<>value
                //          btFormat.SubStrings["ProductName"].Value = “test1”;
                //传入打印机名称
                btFormat.PrintSetup.PrinterName = DefaultPrinter();
                //打印张数
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//Convert.ToInt32(print_sl_txt.Text);
                //     MessageBox.Show("开始打印");

                Messages messages;
                string appName = "下标打印任务";
                Result result = btFormat.Print(appName, 10000, out messages);
                string messageString = "\n\nMessages:";

                foreach (Message message in messages)
                {
                    messageString += "\n\n" + message.Text;
                }

                if (result == Result.Failure)
                    Console.Write("Print Failed" + messageString, appName);
                else
                    Console.Write("文本已成功发送到打印机" + messageString, appName);
            }

   


        }
        private string getQty(string spec)
        {
            // 0402 每卷是50K，每盒是250K，0603 / 0805 / 1206每卷是20K，每盒是100K
            string qty = "";
            switch (spec)
            {
                case "0201":


                    qty = "15000";


                    break;

                case "0402":


                    qty = "10000";


                    break;

                case "0603":

                    qty = "5000";

                    break;

                case "0805":

                    qty = "5000";

                    break;

                case "1206":

                    qty = "5000";

                    break;
                case "1210":

                    qty = "5000";


                    break;



                case "2010":

                    qty = "4000";

                    break;

                case "2512":
                    qty = "4000";

                    break;
                default:
                    break;
            }
            return qty;
        }
        private string getPower(string type, string spec)
        {
            string power = "";
            switch (spec)
            {
                case "0201":
                    switch (type)
                    {
                        case "RC":
                            power = "1/20W";
                            break;
                        case "HP":
                            power = " ";
                            break;
                        case "HV":
                            power = " ";
                            break;
                        case "HS":
                            power = "1/20W";
                            break;
                        case "AS":
                            break;
                        case "LE":
                            break;
                    }
                    break;
                case "0402":
                    switch (type)
                    {
                        case "RC":
                            power = "1/16W";
                            break;
                        case "HP":
                            power = "1/10W";
                            break;
                        case "HV":
                            power = " ";
                            break;
                        case "HS":
                            power = "1/16W";
                            break;
                        case "AS":
                            break;
                        case "LE":
                            break;
                    }
                    break;
                case "0603":
                    switch (type)
                    {
                        case "RC":
                            power = "1/10W";
                            break;
                        case "HP":
                            power = "1/5W";
                            break;
                        case "HV":
                            power = "1/10W";
                            break;
                        case "HS":
                            power = "1/10W";
                            break;
                        case "AS":
                            power = "1/4W";
                            break;
                        case "LE":
                            power = "1/10W";
                            break;
                    }
                    break;
                case "0805":
                    switch (type)
                    {
                        case "RC":
                            power = "1/8W";
                            break;
                        case "HP":
                            power = "1/3W";
                            break;
                        case "HV":
                            power = "1/8W";
                            break;
                        case "HS":
                            power = "1/8W";
                            break;
                        case "AS":
                            power = "1/2W";
                            break;
                        case "LE":
                            power = "1/8W";
                            break;
                    }
                    break;
                case "1206":
                    switch (type)
                    {
                        case "RC":
                            power = "1/4W";
                            break;
                        case "HP":
                            power = "1/2W";
                            break;
                        case "HV":
                            power = "1/4W";
                            break;
                        case "HS":
                            power = "1/4W";
                            break;
                        case "AS":
                            power = "0.6W";
                            break;
                        case "LE":
                            power = "1/4W";
                            break;
                    }
                    break;
                case "1210":
                    switch (type)
                    {
                        case "RC":
                            power = "1/2W";
                            break;
                        case "HP":
                            power = "3/4W";
                            break;
                        case "HV":
                            power = "1/2W";
                            break;
                        case "HS":
                            power = "1/2W";
                            break;
                        case "AS":
                            power = "3/4W";
                            break;
                        case "LE":
                            power = "";
                            break;
                    }
                    break;
                case "2010":
                    switch (type)
                    {
                        case "RC":
                            power = "3/4W";
                            break;
                        case "HP":
                            power = "1W";
                            break;
                        case "HV":
                            power = "3/4W";
                            break;
                        case "HS":
                            power = "3/4W";
                            break;
                        case "AS":
                            power = "1.5W";
                            break;
                        case "LE":
                            power = "";
                            break;
                    }
                    break;
                case "2512":
                    switch (type)
                    {
                        case "RC":
                            power = "1W";
                            break;
                        case "HP":
                            power = "2W";
                            break;
                        case "HV":
                            power = "1W";
                            break;
                        case "HS":
                            power = "1W";
                            break;
                        case "AS":
                            power = "2W";
                            break;
                        case "LE":
                            power = "";
                            break;
                    }
                    break;
            }
            return power;
        }
        private string changeString(string str)
        {


            StringBuilder sb = new StringBuilder(str);
            sb.Replace("0", "A", 3, 1);
            sb.Replace("1", "B", 3, 1);
            sb.Replace("2", "C", 3, 1);
            sb.Replace("3", "D", 3, 1);
            sb.Replace("4", "E", 3, 1);
            sb.Replace("5", "F", 3, 1);
            sb.Replace("6", "G", 3, 1);
            sb.Replace("7", "H", 3, 1);
            sb.Replace("8", "I", 3, 1);
            sb.Replace("9", "J", 3, 1);
            str = sb.ToString();


            return str;
        }
        private string changeString(string str,int index)
        {


            StringBuilder sb = new StringBuilder(str);
            sb.Replace("0", "A", index, 1);
            sb.Replace("1", "B", index, 1);
            sb.Replace("2", "C", index, 1);
            sb.Replace("3", "D", index, 1);
            sb.Replace("4", "E", index, 1);
            sb.Replace("5", "F", index, 1);
            sb.Replace("6", "G", index, 1);
            sb.Replace("7", "H", index, 1);
            sb.Replace("8", "I", index, 1);
            sb.Replace("9", "J", index, 1);
            str = sb.ToString();


            return str;
        }

    }
    }
